<?php
    require_once($_SERVER["DOCUMENT_ROOT"]."/modules/base_mod.php");
    require_once($_SERVER["DOCUMENT_ROOT"]."/modules/channel.php");
    
    class ChannelMod extends BaseMod {
        public static function getChannels(){
            $conn = self::getConnection();
            $sql = "select acc.id, acc.name,  (select count(*) from ajax_chat_online where channel = acc.id) 'count' from ajax_chat_channels acc order by acc.name";
            
            $channels = array();
            $result = mysql_query($sql, $conn);
            
            while ($row = mysql_fetch_assoc($result)) {
                $chn = new Channel(
                                    $row["id"],
                                    $row["name"],
                                    $row["count"]
                                );
                $channels[$chn->getId()] = $chn;
            }
            
            return $channels;
        }
        
        public static function getChannel($id){
            $conn = self::getConnection();
            $sql = "select acc.id, acc.name,  (select count(*) from ajax_chat_online where channel = acc.id) 'count' from ajax_chat_channels acc where acc.id = ".$id;
            
            $result = mysql_query($sql, $conn);
            if($result){
                $row = mysql_fetch_assoc($result);
                return new Channel(
                                    $row["id"],
                                    $row["name"],
                                    $row["count"]
                        );
            }
            
            return null;
        }
        
        public static function getAvailableUserChannels($pid){
            $conn = self::getConnection();
            $sql = "select * from ajax_chat_channels where id not in (select chat_id from ajax_chat_channel_users where pilot_id = ".$pid.") order by name";
            
            $userChannels = array();
            $result = mysql_query($sql, $conn);
            
            while ($row = mysql_fetch_assoc($result)) {
                $uch = new Channel($row["id"], $row["name"]);
                $userChannels[$uch->getId()] = $uch;
            }
            
            return $userChannels;
        }
        
        public static function getUserChannels($pid){
            $conn = self::getConnection();
            $sql = "select * from ajax_chat_channels where id in (select chat_id from ajax_chat_channel_users where pilot_id = ".$pid.") order by name";
            
            $userChannels = array();
            $result = mysql_query($sql, $conn);
            
            while ($row = mysql_fetch_assoc($result)) {
                $uch = new Channel($row["id"], $row["name"]);
                $userChannels[$uch->getId()] = $uch;
            }
            
            return $userChannels;
        }
        
        public static function getChannelUsers($id){
            $conn = self::getConnection();
            $sql = "select accu.pilot_id 'pid', acc.name 'name' from ajax_chat_channel_users accu inner join account acc on acc.pid = accu.pilot_id where chat_id = ".$id." order by acc.name";
            
            $channelUsers = array();
            $result = mysql_query($sql, $conn);
            
            while ($row = mysql_fetch_assoc($result)) {
                $chu = new ChannelUser($row["pid"], $row["name"]);
                $channelUsers[$chu->getId()] = $chu;
            }
            
            return $channelUsers;
        }
        
        public static function getUnsignedUsers($id){
            $conn = self::getConnection();
            $sql = "select pid, name from account where pid not in (select pilot_id from ajax_chat_channel_users where chat_id = ".$id.") and pid > 1 order by name";
            
            $users = array();
            $result = mysql_query($sql, $conn);
            
            while ($row = mysql_fetch_assoc($result)) {
                $chu = new ChannelUser($row["pid"], $row["name"]);
                $users[$chu->getId()] = $chu;
            }
            
            return $users;
        }
        
        public static function addChannel($name){
            $conn = self::getConnection();
            $sql = "select max(id) 'id' from ajax_chat_channels";
            
            $result = mysql_query($sql, $conn);
            if($result){
                $row = mysql_fetch_assoc($result);
                $id = intval($row["id"]) + 1;
            }
            else
                $id = 1;
            $sql = sprintf("insert into ajax_chat_channels (id, name) values (%d, '%s')",
                           $id,
                           mysql_real_escape_string($name)
                          );
            mysql_query($sql, $conn);
        }
        
        public static function updateChannel($id, $name){
            $conn = self::getConnection();
            $sql = sprintf(
                            "update ajax_chat_channels set name = '%s' where id = %d",
                            mysql_real_escape_string($name),
                            intval($id)
                          );
            mysql_query($sql, $conn);
        }
        
        public static function deleteChannel($id){
            $conn = self::getConnection();
            // Start by deleting any currently online users in the channel:
            $sql = "delete from ajax_chat_online where channel = ".$id;
            mysql_query($sql, $conn);
            // Then delete the channel proper:
            $sql = "delete from ajax_chat_channels where id = ".$id;
            mysql_query($sql, $conn);
        }
        
        public static function updateUserChannels($pid, $channels){
            $conn = self::getConnection();
            // First, delete all channels assigned to this user:
            $sql = "delete from ajax_chat_channel_users where pilot_id = ".$pid;
            mysql_query($sql, $conn);
            // Now, let's add the channels:
            foreach ($channels as $channel){
                $sql = "insert into ajax_chat_channel_users (chat_id, pilot_id) values(".$channel.",".$pid.")";
                mysql_query($sql, $conn);
            }
        }
        
        public static function updateChannelUsers($id, $users){
            $conn = self::getConnection();
            // First, delete all users assigned to the channel:
            $sql = "delete from ajax_chat_channel_users where chat_id = ".$id;
            mysql_query($sql, $conn);
            // Now let's add the users:
            foreach ($users as $user){
                $sql = "insert into ajax_chat_channel_users (chat_id, pilot_id) values(".$id.",".$user.")";
                mysql_query($sql, $conn);
            }
        }
    }
?>
